package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentHomeQualityBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.ui.adapter.QualityMemberAdapter;
import com.sgb.member.view_model.QualityMemberModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class HomeQualityMemberFragment extends AppLazyFragment {

    @Inject
    public QualityMemberModel sModel;
    private FragmentHomeQualityBinding mBinding;
    private QualityMemberAdapter mQualityAdapter;

    public static HomeQualityMemberFragment newInstance(int position) {
        HomeQualityMemberFragment fragment = new HomeQualityMemberFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_quality, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        registerObserver();
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mQualityAdapter = new QualityMemberAdapter(getContext(), new ArrayList<>());
        mBinding.recyclerView.setAdapter(mQualityAdapter);
        mQualityAdapter.setOnItemClick(new AdapterOnItemClick<QualityMemberBean>() {
            @Override
            public void onItemClick(QualityMemberBean memberBean, int position) {
                /*Intent intent = new Intent(getActivity(), ShopInfoForCompanyTradesActivity.class);
                if (memberBean.getSupplierId() != null) {
                    intent.putExtra("id", memberBean.getSupplierId());
                    intent.putExtra("companyNo", memberBean.getSupplier());
                    intent.putExtra("position", 5);
                    startActivity(intent);
                } else {
                    MToast.showToast(getActivity(), "店铺id为空");
                }*/
                if (!TextUtils.isEmpty(memberBean.getSupplier())) {
                    MemberConfig.getInstance().getInstance().startWebsiteInfo(getActivity(), String.valueOf(memberBean.getSupplier()), 1);
                } else {
                    MToast.showToast(getActivity(), "店铺No为空");
                }

            }
        });
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
    }


    public void loadData(boolean isLoadMore) {
        if (sModel != null) {
            sModel.getRecommendedMembers();
        }
    }

    private void registerObserver() {

    }

    public void closeRecyclerRefreshing() {
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof HomeMemberFragment) {
            ((HomeMemberFragment) parentFragment).closeRecyclerRefreshing();
        }
    }

    public void showEmpty() {
        if (mQualityAdapter != null) {
            mQualityAdapter.clearDatas();
            mBinding.emptyView.setVisibility(View.VISIBLE);
        }
    }

    public void refreshRecommendedRecycler(List<QualityMemberBean> data) {
        closeRecyclerRefreshing();
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        if (data != null && data.size() > 0) {
            mBinding.emptyView.setVisibility(View.GONE);
            mQualityAdapter.setDatas(data);
        } else {
            mQualityAdapter.clearDatas();
            mBinding.emptyView.setVisibility(View.VISIBLE);
        }
    }
}
